﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Habi.Solver.Models.Resources;

namespace Habi.Solver.Views.Resources
{
    public partial class ResourceTaskItemControl : XtraUserControl, IMessageFilter
    {
        [Browsable(true)]
        public bool Checked { get; set; }

        public ResourceTaskItemControl(ResourceTaskModel model)
        {
            InitializeComponent();

            IconIndex = new Dictionary<string, int>();
            int index = 0;
            foreach (var item in svgImageCollection.Keys)
            {
                IconIndex.Add(item, index++);
            }

            bindingSource.DataSource = model;

            Icon = model.Icon;
            FileName = model.Url;
            FileSize = model.Size;

            RegistEvent(Controls, 0);
        }

        private void RegistEvent(ControlCollection controls, int deep)
        {
            if (controls == null || controls.Count == 0 || deep > 4) return;

            foreach (Control item in controls)
            {
                item.MouseHover += Item_MouseHover;
                item.MouseLeave += Item_MouseLeave;
                item.MouseClick += Item_MouseClick;

                RegistEvent(item.Controls, deep++);
            }
        }

        private void Item_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left) return;

            Checked = !Checked;
            if (Checked)
            {
                BackColor = Color.FromArgb(255, 91, 150, 194);
                ForeColor = Color.FromArgb(255, 0xFF, 0xFF, 0xFF);
            }
            if (!Checked)
            {
                BackColor = Color.FromArgb(255, 232, 232, 232);
                ForeColor = Color.FromArgb(255, 60, 60, 60);
            }
        }

        private void Item_MouseLeave(object sender, EventArgs e)
        {
            if (!Checked) BackColor = Color.FromArgb(255, 232, 232, 232);
        }

        private void Item_MouseHover(object sender, EventArgs e)
        {
            if (!Checked) BackColor = Color.FromArgb(255, 186, 202, 222);
        }

        private readonly Dictionary<string, int> IconIndex;

        public ResourceTaskItemControl()
        {
            InitializeComponent();

            IconIndex = new Dictionary<string, int>();
            int index = 0;
            foreach (var item in svgImageCollection.Keys)
            {
                IconIndex.Add(item, index++);
            }
        }

        private int IndexOfIcon(string icon)
        {
            if (string.IsNullOrEmpty(icon)) return 0;

            if (IconIndex.ContainsKey(icon)) return IconIndex[icon];

            return 0;
        }

        private string icon = "FILE";

        [Browsable(true)]
        public string Icon
        {

            get
            {
                return icon;
            }

            set
            {
                icon = value;

                layoutControlItemForIcon.ImageOptions.ImageIndex = IndexOfIcon(value);
            }
        }


        [Browsable(true)]
        public string FileName
        {
            get
            {
                return labelControlFileName.Text;
            }

            set
            {
                labelControlFileName.Text = value;
            }
        }

        [Browsable(true)]
        public int Percent
        {
            get
            {
                return progressBarControl.Properties.Step;
            }
            set
            {
                progressBarControl.Position = value;
                labelControlProgress.Text = string.Format("{0}%", value);
            }
        }


        private double fileSize = 0;

        [Browsable(true)]
        public double FileSize
        {
            get
            {
                return fileSize;
            }
            set
            {
                fileSize = value;
                labelFileSize.Text = UnitOfFileSize(value);
            }
        }

        private string UnitOfFileSize(double size)
        {
            string[] units = new string[] { "B", "KB", "MB", "GB", "TB", "PB" };
            double mod = 1024.0;
            int i = 0;
            while (size >= mod)
            {
                size /= mod;
                i++;
            }
            return Math.Round(size) + units[i];
        }

        public bool PreFilterMessage(ref Message m)
        {

            Console.WriteLine($"Msg: {m.Msg}");

            return true;
        }
    }
}
